//
//  CZStatusRetweetView.swift
//  CZWeibo
//
//  Created by 周振兰 on 16/5/23.
//  Copyright © 2016年 周振兰. All rights reserved.
//

import UIKit
//显示被转发微博内容
class CZStatusRetweetView: UIView {
    
    
    ///被转发模型
    var reteetStatus:CZStatus?
        {
        didSet{
            if reteetStatus == nil{
                retweetContentLabel.text = nil
            }else{
                // 设置被转发微博内容
                // @被转发微博用户的名称: 被转发微博的内容
               let name = reteetStatus?.user?.screen_name ?? "未知的名称"
                let text = reteetStatus?.text ?? ""
                
                retweetContentLabel.text = "@\(name):\(text)"
            }
            
             // 设置被转发的图片图片,内部会自动更新大小
            retweetPictureView.pictureURLs = reteetStatus?.pictureURLs
            
             // 如果没有被转发微博,还是会显示被转发的view
            /// 重新更新retweetContentLabel.top间距
            
            retweetContentLabel.snp_updateConstraints { (make) -> Void in
                let margin = reteetStatus == nil ? 0 : CellMargin
                make.top.equalTo(self).offset(margin)
            }
            
            ///重新更新配图的顶部
            retweetPictureView.snp_makeConstraints { (make) -> Void in
                var margin: CGFloat = CellMargin
                if reteetStatus == nil{
                    
                    //没有被转发微博，不要顶部间距
                    margin = 0
                    
                }else if reteetStatus?.pictureURLs == nil || reteetStatus?.pictureURLs?.count == 0{
                    margin = 0
                }
                
                make.top.equalTo(retweetContentLabel.snp_bottom).offset(margin)
                
            }
            ///被转发view的底部
            
            self.snp_updateConstraints { (make) -> Void in
                let margin = reteetStatus == nil ? 0 : CellMargin
                make.bottom.equalTo(retweetPictureView).offset(margin)
                
            }
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    private func prepareUI(){
        
        backgroundColor = UIColor.clearColor()
        //添加控件
        addSubview(retweetContentLabel)
        addSubview(retweetPictureView)
        // 添加约束, 注意如果添加约束的这个控件没有父控件,添加不了约束
        /// 被转发的微博文本
        retweetContentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(CellMargin)
            make.top.equalTo(self).offset(CellMargin)
            
            //被转发的配图
            retweetPictureView.snp_updateConstraints(closure: { (make) -> Void in
                make.leading.equalTo(self).offset(CellMargin)
                make.top.equalTo(retweetContentLabel.snp_bottom).offset(CellMargin)
                make.size.equalTo(CGSize(width: 200, height: 200))
                
            })
            
            ///底部约束
            ///转发底部参照配图底部偏移8
            self.snp_makeConstraints(closure: { (make) -> Void in
              make.bottom.equalTo(retweetPictureView).offset(CellMargin).constraint
            })
        }
        
    }
    
    //MARK: - 懒加载 
    ///被转发微博的内容
    private lazy var retweetContentLabel:UILabel = {
        let label = UILabel()
        //设置
        label.textColor = UIColor.darkGrayColor()
         label.font = UIFont.systemFontOfSize(CellStatusFontSize)
        label.numberOfLines = 0
        
        
        // 最大宽度
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * CellMargin
       
        return label
    }()
    
  ///被转发的配图
      private lazy var retweetPictureView: CZStatusPictureView = CZStatusPictureView()
}
